package neo.aoi.octree;

import javax.swing.SwingWorker;

import neo.aoi.ExportProgressListener;
import artofillusion.Scene;
import buoy.widget.BStandardDialog;
import buoy.widget.WindowWidget;

/**
 * This class implements a SwingWorker thread which performs the voxel export
 * process. This class internally uses the VoxelExporter class and only provides
 * a way to wrap this method in a SwingWorker thread which can then be run using
 * the <code>SwingWorker.execute()</code> method
 * 
 * @see <a
 *      http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html">Concurrency
 *      in Swing</a>
 * 
 * @author Mauro Santos
 */
public class OctreeExportTask extends SwingWorker<Void, Void> {
	private ExportProgressListener _progressListener;

	private WindowWidget _parent;

	private OctreeExporter.ExportParameters _params;

	private Scene _theScene;

	/**
	 * Creates a new VoxelExportTask which will export the given Scene using the
	 * provided parameters. The given ProgressListener will be used to report
	 * the progress of the export process. When the process is finished, a
	 * dialog window will display a success/error message. The dialog will be
	 * created using the given WindowWidget as its parent.
	 * 
	 * @param params
	 *            The parameters to be passed to the
	 *            <code>VoxelExporter.doExport</code> method
	 * @param theScene
	 *            The Scene to be exported
	 * @param parent
	 *            The WindowWidget object which will be parent to any message
	 *            displayed by this object.
	 * @param progress
	 *            The ExportProgressListener to which the progress will be
	 *            reported.
	 */
	public OctreeExportTask(OctreeExporter.ExportParameters params,
			Scene theScene, WindowWidget parent, ExportProgressListener progress) {
		_progressListener = progress;
		_params = params;
		_parent = parent;
		_theScene = theScene;
	}

	@Override
	protected Void doInBackground() {
		OctreeExporter.ExportResult ret = OctreeExporter.doExport(_params
				.getFileName(), _theScene, _params.getTolerance(), _params
				.getMaxResolution(), _params.fill(), _params.balanceOctree(),_progressListener);

		if (ret.getStatus() == OctreeExporter.ExportResult.STATUS.OK) {
			BStandardDialog dialog = new BStandardDialog(
					"Scene Octree Exporter", ret.getMessage(),
					BStandardDialog.INFORMATION);
			dialog.showMessageDialog(_parent);
		} else {
			new artofillusion.ui.MessageDialog(_parent,
					"Something went wrong, not sure what: \n"
							+ ret.getMessage());
		}

		return null;
	}

	@Override
	public void done() {
		_progressListener.finished();
	}
}
